package com.b.sts.controller;

import com.b.sts.entity.AnnouncementEntity;
import com.b.sts.service.AnnouncementService;
import com.b.sts.util.ResultUtil;
import com.b.sts.vo.ResultVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author b
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
    @Resource
    AnnouncementService service;

    @GetMapping("/{id}")
    public ResultVO<AnnouncementEntity> getById(@PathVariable Long id) {
        AnnouncementEntity data = service.getById(id);
        return ResultUtil.success(data);
    }

    @GetMapping("/list")
    public ResultVO<List<AnnouncementEntity>> list() {
        List<AnnouncementEntity> data = service.list();
        return ResultUtil.success(data);
    }

    @GetMapping("/page")
    public ResultVO<Page<AnnouncementEntity>> page(@RequestParam(defaultValue = "1") int pageNum,
                                                   @RequestParam(defaultValue = "10") int pageSize) {
        Page<AnnouncementEntity> data = service.page(new Page<>(pageNum, pageSize));
        return ResultUtil.success(data);
    }

    @PostMapping
    public ResultVO<Boolean> add(@RequestBody AnnouncementEntity AnnouncementEntity) {
        boolean isSuccess = service.save(AnnouncementEntity);
        return ResultUtil.autoDo(isSuccess);
    }

    @DeleteMapping("/{id}")
    public ResultVO<Boolean> del(@PathVariable Long id) {
        boolean isSuccess = service.removeById(id);
        return ResultUtil.autoDo(isSuccess);
    }

    @PutMapping
    public ResultVO<Boolean> update(@RequestBody AnnouncementEntity AnnouncementEntity) {
        boolean isSuccess = service.updateById(AnnouncementEntity);
        return ResultUtil.autoDo(isSuccess);
    }
}
